package com.slipper.bpmn.controller;

import com.slipper.bpmn.vo.BpmnVO;
import com.slipper.util.*;
import com.slipper.system.employee.bo.EmployeeBO;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.slipper.system.bpmn.bo.BpmnBO;
import com.slipper.system.bpmn.service.BpmnService;
import com.slipper.constants.Constants;
import com.slipper.common.bo.UserDetail;

/**
 * 流程服务
 */
@RestController
@RequestMapping("/flow/bpmn")
public class BpmnController {
    @Resource
    private BpmnService bpmnService;

    @PostMapping("/queryBy")
    public String queryBy(@RequestBody BpmnBO bo) throws Exception {
        UserDetail userDetail = Constants.getCurrentUser();
        EmployeeBO employee = userDetail.getEmployee();
        bo.setCurrOrgId(userDetail.getOrgId());
        bo.setCurrOrgName(userDetail.getOrgName());
        bo.setCurrEmpId(employee.getId());
        bo.setCurrEmpName(employee.getName());
        bo.setCurrEmpSex(employee.getSex());
        return ResultUtil.successResult(bpmnService.queryBy(bo));
    }

    @PostMapping("/queryNodeDataList")
    public String queryNodeDataList(@RequestBody BpmnBO bo) {
        return ResultUtil.successResult(bpmnService.queryNodeDataList(bo));
    }
    
    @PostMapping("/save")
    public String save(@RequestBody BpmnVO vo) {
        UserDetail userDetail = Constants.getCurrentUser();
        vo.setCurrOrgId(userDetail.getOrgId());
        vo.setCurrEmpId(userDetail.getEmployeeId());
        return bpmnService.save(vo);
    }

    @PostMapping("/saveCompleted")
    public String saveCompleted(@RequestBody BpmnVO vo) {
        UserDetail userDetail = Constants.getCurrentUser();
        vo.setCurrOrgId(userDetail.getOrgId());
        vo.setCurrEmpId(userDetail.getEmployeeId());
        return bpmnService.saveCompleted(vo);
    }

    @PostMapping("/saveReturn")
    public String saveReturn(@RequestBody BpmnVO vo) {
        UserDetail userDetail = Constants.getCurrentUser();
        vo.setCurrOrgId(userDetail.getOrgId());
        vo.setCurrEmpId(userDetail.getEmployeeId());
        return bpmnService.saveReturn(vo);
    }

    @PostMapping("/saveRetrieve")
    public String saveRetrieve(@RequestBody BpmnVO vo) {
        UserDetail userDetail = Constants.getCurrentUser();
        vo.setCurrOrgId(userDetail.getOrgId());
        vo.setCurrEmpId(userDetail.getEmployeeId());
        return bpmnService.saveRetrieve(vo);
    }

    /**
     * 转单
     */
    @PostMapping("/transfer")
    public String transfer(@RequestBody BpmnBO bo) {
        if (Tool.isBlank(bo.getReceiveManId()) 
                || Tool.isBlank(bo.getInstNodeId())) {
            return ResultUtil.failureResult("参数不正确！");
        }
        
        bo.setCurrEmpId(Constants.getCurrentUser().getEmployeeId());
        return bpmnService.transfer(bo);
    }
}
